package com.zhuss.product.processor;

import java.util.Date;

import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.PrelineSignDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.preline.StopPrelineBo;
import com.zhuss.bean.vo.product.preline.DetailPrelineVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.StartStopEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.product.db.dao.ext.PrelineDao;
import com.zhuss.product.db.entity.Preline;
import com.zhuss.util.JsonUtil;

/**
 * 创建招募 processor
 * @Author: zhushanshuo
 * @Date: Wed Jan 06 16:56:43 CST 2021
 */
@Component("prelineStop")
public class PrelineStop extends AbstractProcessor<Object, StopPrelineBo> implements Processor<Object, StopPrelineBo> {

    @Autowired
    private PrelineDao prelineDao;

    @Autowired
	@Qualifier("prelineRead")
	private PrelineRead prelineRead;

    @Autowired
    private PrelineCheck prelineCheck;

	@Autowired
	private PrelineSignDao prelineSignDao;

    /**
     * 校验
     */
    @Override
    public void check(StopPrelineBo bo) throws DzcxException {
    	// 当前招募不满足停用条件
//		if (!prelineCheck.checkStop(bo.getPrelineId())) {
//			throw new DzcxException(ExceptionCode.当前招募不满足停用条件);
//		}

		//线路有报名不让删除
		if (prelineSignDao.countByPrelineId(bo.getPrelineId()) > 0) {
			throw new DzcxException(ExceptionCode.已有报名信息);
		}
    }

    /**
     * 操作方法
     * @throws DzcxException
     */
    @Override
    public Object process(StopPrelineBo param) throws DzcxException {

    	Long prelineId = param.getPrelineId();

    	Preline record = new Preline();
    	record.setPrelineId(param.getPrelineId());
    	record.setStatus(StartStopEnum.停用.key());

    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	prelineDao.updateByPrimaryKeySelective(record);

    	String name = ((DetailPrelineVo) prelineRead.detail(prelineId)).getName();

    	param.setName(name);
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(StopPrelineBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品.key());
    	record.setAction(LogActionEnum.招募新线停用.key());
    	record.setTargetId(param.getPrelineId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
